package me.zhengjie.modules.vip.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.vip.domain.RechargeRecord;
import me.zhengjie.modules.vip.service.RechargeRecordService;
import me.zhengjie.modules.vip.domain.vo.RechargeRecordQueryCriteria;
import me.zhengjie.modules.vip.mapper.RechargeRecordMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class RechargeRecordServiceImpl extends ServiceImpl<RechargeRecordMapper, RechargeRecord> implements RechargeRecordService {

    @Override
    public PageResult<RechargeRecord> queryAll(RechargeRecordQueryCriteria criteria, Page<Object> page) {
        IPage<RechargeRecord> queryPage = lambdaQuery()
            .eq(criteria.getCardId() != null, RechargeRecord::getCardId, criteria.getCardId())
            .eq(criteria.getMemberId() != null, RechargeRecord::getMemberId, criteria.getMemberId())
            .eq(criteria.getPaymentMethod() != null, RechargeRecord::getPaymentMethod, criteria.getPaymentMethod())
            .eq(criteria.getCreateBy() != null, RechargeRecord::getCreateBy, criteria.getCreateBy())
            .orderByDesc(RechargeRecord::getCreateTime)
            .page(new Page<>(page.getCurrent(), page.getSize()));
        return new PageResult<>(queryPage.getRecords(), queryPage.getTotal());
    }

    @Override
    public List<RechargeRecord> queryAll(RechargeRecordQueryCriteria criteria) {
        return lambdaQuery()
            .eq(criteria.getCardId() != null, RechargeRecord::getCardId, criteria.getCardId())
            .eq(criteria.getMemberId() != null, RechargeRecord::getMemberId, criteria.getMemberId())
            .eq(criteria.getPaymentMethod() != null, RechargeRecord::getPaymentMethod, criteria.getPaymentMethod())
            .eq(criteria.getCreateBy() != null, RechargeRecord::getCreateBy, criteria.getCreateBy())
            .orderByDesc(RechargeRecord::getCreateTime)
            .list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(RechargeRecord resources) {
        save(resources);
    }

    @Override
    public void download(List<RechargeRecord> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RechargeRecord record : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("会员卡ID", record.getCardId());
            map.put("会员ID", record.getMemberId());
            map.put("充值金额", record.getAmount());
            map.put("赠送金额", record.getGiftAmount());
            map.put("充值前余额", record.getBeforeAmount());
            map.put("充值后余额", record.getAfterAmount());
            map.put("支付方式", record.getPaymentMethod());
            map.put("操作人", record.getCreateBy());
            map.put("创建时间", record.getCreateTime());
            map.put("备注", record.getRemarks());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void revoke(Long recordId) {

    }
} 